"""
__author__ = '霍格沃兹测试开发学社'
__desc__ = '更多测试开发技术探讨，请访问：https://ceshiren.com/t/topic/15860'
"""
import allure
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait


class BasePage:
    _BASE_URL = ""

    # 构造方法
    def __init__(self, base_driver=None):
        # 初始化driver
        if base_driver:
            self.driver = base_driver
        else:
            # 实例化driver
            self.driver = webdriver.Chrome()
            self.driver.implicitly_wait(5)
            self.driver.maximize_window()
        if not self.driver.current_url.startswith("http"):
            self.driver.get(self._BASE_URL)

    def do_find(self, by, value=None):
        """获取单个元素"""
        if value:
            return self.driver.find_element(by, value)
        else:
            return self.driver.find_element(*by)

    def do_finds(self, by, value=None):
        """获取多个元素"""
        if value:
            return self.driver.find_elements(by, value)
        else:
            return self.driver.find_elements(*by)

    def do_send_keys(self, text, by, value=None):
        """输入文本"""
        ele = self.do_find(by, value)
        ele.clear()
        ele.send_keys(text)
        self.get_screen()

    def do_quit(self):
        """退出浏览器"""
        self.driver.quit()

    def wait_visible(self, locator: tuple):
        """等待可见"""
        ele = WebDriverWait(self.driver, 10).until(expected_conditions.visibility_of_element_located(locator))
        self.get_screen()
        return ele

    def get_screen(self):
        """截图"""
        self.driver.save_screenshot("tmp.png")
        # 讲截图放到报告的数据中
        allure.attach.file("tmp.png", name="picture",
                           attachment_type=allure.attachment_type.PNG)
